<script src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script>
wx.config({
    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
    appId: '{{.share.Appid}}', // 必填，公众号的唯一标识
    timestamp:{{.share.Timestamp}} , // 必填，生成签名的时间戳
    nonceStr: '{{.share.NonceStr}}', // 必填，生成签名的随机串
    signature: '{{.share.Signature}}',// 必填，签名，见附录1
    jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage','showOptionMenu','chooseImage','uploadImage','previewImage','openLocation','scanQRCode','getLocation'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
});
var share_title={{.system.name}}
var share_desc={{.system.description}}
var share_link={{.system.url}}+"/mobile/index"
var share_img={{.system.pathname}}
{{if .s_title}}
{{if ne .s_title ""}}
share_title={{.s_title}}
{{end}}
{{end}}
{{if .s_desc}}
{{if ne .s_desc ""}}
share_desc={{.s_desc}}
{{end}}
{{end}}
{{if .s_url}}
{{if ne .s_url ""}}
share_link={{.s_url}}
{{end}}
{{end}}
{{if .s_img}}
{{if ne .s_img ""}}
share_img={{.s_img}}
{{end}}
{{end}}
{{if .share_uid}}
{{if ne .share_uid ""}}
share_link=share_link.split("?")
if(share_link[1]){
	share_link=share_link.join("?")
	share_link+="&share_uid="+{{.share_uid}}
}else{
	share_link=share_link[0]+"?share_uid="+{{.share_uid}}
}
{{end}}
{{end}}
// console.log(share_title)
// console.log(share_desc)
// console.log(share_link)
// console.log(share_img)
wx.ready(function () { 
    wx.showOptionMenu()   
    wx.onMenuShareAppMessage({
        title: share_title,
        desc: share_desc,
        link: share_link,
        imgUrl: "{{.system.url}}"+share_img,
        success: function () {
            mui.toast('分享成功~')
        },
        cancel: function () {
            mui.toast('您取消了分享~')
        }
    });
    wx.onMenuShareTimeline({
        title: share_title,
        desc: share_desc,
        link: share_link,
        imgUrl: "{{.system.url}}"+share_img,
        success: function () {
            mui.toast('分享成功~')
        },
        cancel: function () {
            mui.toast('您取消了分享~')
        }
    });
    // {{if .coor}}
    // {{else}}
    wx.getLocation({
        type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
        success: function (res) {
            var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
            var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
            {{if .currentcity}}
            {{else}}
            var pt = new BMap.Point(longitude,latitude)  
            var geoc = new BMap.Geocoder()
            geoc.getLocation(pt, function(rs){
                var addComp = rs.addressComponents
                city = addComp.city
                mui.toast(city)
                sessionStorage.setItem('location', city)
                relocation(city)
            });
            {{end}}
            $.ajax({
                url:'/mobile/coor',
                data:{latitude:latitude,longitude:longitude},
                success:function(res) {
                }
            })
        },
        fail: function (res) {
            sessionStorage.setItem('location', '台州市')
            mui.toast("获取位置失败")
            console.log(res)
        }
    });
    // {{end}}
});

wx.error(function(res){
    mui.toast('授权失败')
    console.log(res)
});

function relocation(city) {
    if (!city) {
        mui.toast("定位失败")
        return
    }
    $.ajax({
        url: location.origin + '/relocation',
        data: {
          city: city
        },
        success: function (res) {
            if (res.status == 1) {
                location.reload()
            } else if (res.status == 0) {
                mui.toast(city)
            } else {
                mui.toast("定位失败")
            }
        }
    })
}
</script>